

//https://www.ozon.ru/api/entrypoint-api.bx/page/json/v2?url=/product/1185794392/?layout_container=pdpPage2column&layout_page_index=2
function getQueryString(name) {
    let reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
    let r = window.location.search.substr(1).match(reg);
    if (r != null) {
        return r[2];
    };
    return 0;
 }

console.log(" get ozon question ok ");

var auto_get = (getQueryString("auto_get"))
console.log(auto_get);
chrome.runtime.onMessage.addListener(async (msg, sender) => {
  console.log(" get ozon detail msg ok ")
  console.log(msg)
  var sku = msg['data']['item']['sku_id'];
  if(!sku){
        console.log("no sku!! no data!!")
        return;
  }
  var app_id = msg['data']['item']['app_id'];
  var prepub_id = msg['data']['item']['id'];

  var url = 'https://www.ozon.ru/api/entrypoint-api.bx/page/json/v2?url=/product/'+sku+'/?layout_container=pdpReviews&layout_page_index=2';
  $.ajax({
    url: url,
　　　　 type: 'GET',
    xhrFields: {
        withCredentials: true // 这里设置了withCredentials
    },
    success: function(data) {
       console.log(data);
       var dataItem = {}
       var widgetStates = data['widgetStates'];
       for (const key in widgetStates) {
          //console.log(key);
          if (key.includes("webSellerList")) {
                dataItem.seller_list = widgetStates[key];
          }else if(key.includes("webCurrentSeller")){
                dataItem.this_seller = widgetStates[key];
          }
        }
       console.log(dataItem);
        /*var type="ozondetail";
        var body = dataItem;
        chrome.runtime.sendMessage({ data: {type , body ,sku}, close: false });*/

        var asyncData = "";
        var widgetStateId = "";
        if(data['layout']){
                data['layout'].forEach((item1) => {
                if(item1.component='webReviewTabs'){
                    if(!item1.placeholders){return;}
                    item1.placeholders.forEach((item2) => {
                         if(item2.name=='tabs'){
                             if(!item2.widgets){return;}
                             item2.widgets.forEach((item3) => {
                                    if(!item3.placeholders){return;}
                                    item3.placeholders.forEach((item4) => {
                                        if(!item4.widgets){return;}
                                        item4.widgets.forEach((item5) => {
                                            if(!item5.placeholders){return;}
                                            item5.placeholders.forEach((item6) => {
                                                    if(!item6.widgets){return;}
                                                    item6.widgets.forEach((item7) => {
                                                        if(item7.component=='webListQuestions'){
                                                                console.log(item7)
                                                                asyncData = item7.asyncData;
                                                                widgetStateId = item7.stateId;
                                                        }
                                                    });
                                            });

                                        });
                                    });
                             });
                         }
                    });
                }
            });
         }

        if(!widgetStateId){
            if(data['pageInfo']){
                $.post("https://www.gongrenbao.cn/index.php?c=api/erp/askUpdate",
                  {data:{},sku_id:sku,prepub_id:prepub_id,app_id:app_id},
                  function(data){
                     console.log(data);
                  },
              "text");
            }
            console.log("widgetStateId null error")
            return;
        }

        var url ="https://www.ozon.ru/api/composer-api.bx/widget/json/v2?widgetStateId="+widgetStateId;
        var asydata = {};
        asydata.asyncData = asyncData;
        asydata.extraBody=true;

         $.ajax({
          type: "POST",
          url: url,
          data: JSON.stringify(asydata),
          contentType: "application/json; charset=utf-8",
          dataType: "json",
          success: function(response) {
            console.log(response);

            $.post("https://www.gongrenbao.cn/index.php?c=api/erp/askUpdate",
                  {data:response,sku_id:sku,prepub_id:prepub_id,app_id:app_id},
                  function(data){
                     console.log(data);
                  },
              "text");
          }
        });

    },
    error: function(xhr, status, error) {

        if (xhr.status === 403 && status === "error") {
            var type="ozonapi403";
            chrome.runtime.sendMessage({ data: {type}, close: false });
            console.log("遇到了403错误");
            //中断
        }else{
            location.reload();
        }

    }
});

})
